﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using Fly.Common.Linq;
using RN.ERP.Web.Utilities;
using System.Configuration;
using RN.ERP.Web.ModelService;
using RN.ERP.Web.PersonService;

namespace RN.ERP.Web.Handlers
{
    /// <summary>
    /// PsnEmpInfoHandlerLC 的摘要说明
    /// </summary>
    public class PsnEmpInfoLCHandler : BaseHandler, IHttpHandler
    {

        public override void ProcessRequest(HttpContext context)
        {
            GetParams(context);
        }
        #region 页面数据操作
        /// <summary>
        /// 获取传递参数
        /// </summary>
        private void GetParams(HttpContext context)
        {
            Guid empGuid = Guid.NewGuid();
            string areaCode = string.Empty;
            if (!string.IsNullOrEmpty(context.Request.Params["empGuid"]))
            {
                Guid.TryParse(context.Request.Params["empGuid"], out empGuid);
            }
            //当前市场
            if (!string.IsNullOrEmpty(context.Request.Params["areaCode"]))
            {
                areaCode = context.Request.Params["areaCode"];
            }
            ////异步调用方法
            if (context.Request.Params["action"] != null)
            {
                string action = context.Request.Params["action"];
                switch (action)
                {
                    case "Qualitie":
                        GetEmpQualities(empGuid.ToString(), areaCode, context);
                        break;
                    case "UserLog":
                        GetUserLog(empGuid.ToString(), areaCode, context);
                        break;
                    case "getMachineInfo":
                        GetMachineInfoByID(context);
                        break;
                    case "getfinancialInfo":
                        GetFinancialInfoByID(context);
                        break;
                }
            }
        }

        /// <summary>
        /// 输出JSON格式数据到页面
        /// </summary>
        /// <param name="dataStr"></param>
        private void OutputData(string dataStr)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(dataStr);
            // HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }


        /// <summary>
        /// 人员资格证书查询
        /// </summary>
        private void GetEmpQualities(string empGuid, string areaCode, HttpContext context)
        {
            string type = "ZGZSLC";////默认显示资格证书
            int isValid = -1;////是否导入    
            string groupId = string.Empty;
            if (context.Request.QueryString["type"] != null)
            {
                type = context.Request.QueryString["type"].Trim();
            }
            if (context.Request.QueryString["isValid"] != null)
            {
                int.TryParse(context.Request.QueryString["isValid"].Trim(), out isValid);
            }
            if (context.Request.QueryString["groupId"] != null)
            {
                groupId = context.Request.QueryString["groupId"].Trim();
            }
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                var list = JsonConvert.DeserializeObject<List<ModelService.Tb_Emp_TitleDetails>>(client.GetEmpQualitiesByGroupId(empGuid, areaCode, groupId));
                list = list.Where(q => q.StampCode == type).ToList();
                if (isValid >= 0)
                {
                    list = list.Where(q => q.IsValid == isValid).ToList();
                }
                OutputData(JsonConvert.SerializeObject(list));
            }
        }
        /// <summary>
        /// 人员资格证书删除
        /// </summary>
        private void EmpQualityMultiDel(string empGuid, string areaCode, HttpContext context)
        {
            string educationGUID = string.Empty;
            if (!string.IsNullOrEmpty(context.Request.Params["keyId"]))
            {
                educationGUID = context.Request.Params["keyId"];
                using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
                {
                    OutputData(client.EmpQualityMultiDel(educationGUID, empGuid, areaCode));
                }
            }
        }

        /// <summary>
        /// 查询操作日志
        /// </summary>
        private void GetUserLog(string certGuid, string areaCode, HttpContext context)
        {
            int starNum = 1;//当前页
            int pageSize = 5;//每页数
            string userRole = "User";
            if (context.Request.Params["pageNo"] != null)
            {
                int.TryParse(context.Request.Params["pageNo"].ToString(), out starNum);
            }
            if (context.Request.Params["pageSize"] != null)
            {
                int.TryParse(context.Request.Params["pageSize"].ToString(), out pageSize);
            }
            if (context.Request.Params["userRole"] != null)
            {
                userRole = context.Request.Params["userRole"];
            }
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                int allCount = client.GetUserLogCount(certGuid, userRole, areaCode);
                string allList = client.GetUserLogList(starNum, pageSize, certGuid, userRole, areaCode);
                string result = "{\"pageCount\":" + allCount + ",\"data\":" + allList + "}";
                OutputData(result);

            }
        }


        /// <summary>
        /// 根据ID获取企业设备基本信息
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private void GetMachineInfoByID(HttpContext context)
        {
            try
            {
                int id = Convert.ToInt32(context.Request["mid"]);
                EnterpriseService.EnterpriseServiceClient client = new EnterpriseService.EnterpriseServiceClient();
                string result = client.GetCorpMachineInfoByID(new Guid(UserInfo.CorpUserGuid), id, UserInfo.CurrentArea);
                string rst = JsonConvert.SerializeObject(new { Result = result, Message = "获取成功" });
                OutputData(rst);
            }
            catch (Exception ex)
            {
                string rst = JsonConvert.SerializeObject(new { Result = false, Message = "获取失败" });
                OutputData(rst);
            }
        }

        /// <summary>
        /// 根据ID获取企业财务基本信息
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private void GetFinancialInfoByID(HttpContext context)
        {
            //try
            //{
            int id = Convert.ToInt32(context.Request["mid"]);
            using(EnterpriseService.EnterpriseServiceClient client = new EnterpriseService.EnterpriseServiceClient())
            {
                string result = client.GetCorpFinancialInfoByID(new Guid(UserInfo.CorpUserGuid), id, UserInfo.CurrentArea);
                //string rst = JsonConvert.SerializeObject(new { Result = result, Message = "获取成功" });
                var rst = "{\"result\":1,\"data\":" + result + "}";
                OutputData(result);
            }
            //}
            //catch (Exception ex)
            //{
            //    string rst = JsonConvert.SerializeObject(new { Result = false, Message = "获取失败" });
            //    OutputData(rst);
            //}
        }    

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        #endregion
    }
}